//
//  MyQRCodeVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/21.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "MyQRCodeVC.h"
#import "UIImage+LXQRCode.h"
#import <SDImageCache.h>
#import <SDWebImageDownloader.h>

@interface MyQRCodeVC ()

@property (weak, nonatomic) IBOutlet UIView *pContentView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *mobileLabel;
@property (weak, nonatomic) IBOutlet UIImageView *qrImgView;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;

@end

@implementation MyQRCodeVC
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *icon = nil;
    NSString *jsonStr = nil;
    NSString *iconUrl = nil;
    if (self.vcType == 1) {
        self.title = @"群二维码";
        self.bottomLabel.text = @"扫二维码，加入群聊";
        self.nameLabel.text = self.group.name;
        self.mobileLabel.text = [NSString stringWithFormat:@"ID: %@", self.group.group_id];
        [self.iconImgView sd_setImageWithURL:self.group.avatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
        icon = [[SDImageCache sharedImageCache] imageFromCacheForKey:self.group.avatar];
        iconUrl = self.group.avatar;
        jsonStr = self.group.getGroupCardJson;
        
    } else {
        self.title = @"我的二维码";
        self.bottomLabel.text = @"扫二维码，加我好友";
        self.nameLabel.text = kUsermanager.me.username;
        self.mobileLabel.text = [NSString stringWithFormat:@"ID: %@", kUsermanager.me.ID];
        [self.iconImgView sd_setImageWithURL:kUsermanager.me.avatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
        icon = [[SDImageCache sharedImageCache] imageFromCacheForKey:kUsermanager.me.avatar];
        iconUrl = kUsermanager.me.avatar;
        jsonStr = kUsermanager.me.getMyCardJson;
        
    }
    self.pContentView.layer.cornerRadius = 5.0f;
    self.iconImgView.layer.cornerRadius = 35;
    self.iconImgView.layer.masksToBounds = YES;
    
    jsonStr = [NSString stringWithFormat:@"zhongyilianchuang:%@",jsonStr];
    if (icon) {
        
        UIImage *qrImg = [UIImage LX_ImageOfQRFromURL:jsonStr codeSize:152 logoName:icon radius:3.0f borderWidth:2 borderColor:UIColor.whiteColor];
        self.qrImgView.image = qrImg;
    } else {
        [MBProgressHUD showActivityMessageInView:@""];
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:iconUrl.toURL completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            [MBProgressHUD hideHUD];
            if (image == nil) {
                image = UIImage.k_imgName(@"placeholder_user");
            }
            UIImage *qrImg = [UIImage LX_ImageOfQRFromURL:jsonStr codeSize:152 logoName:image radius:3.0f borderWidth:2 borderColor:UIColor.whiteColor];
            self.qrImgView.image = qrImg;
        }];
    }
    
}


@end
